//
//  HomeViewCell.swift
//  Swift微博
//
//  Created by adel on 2017/8/28.
//  Copyright © 2017年 com.bailun91. All rights reserved.
//pod install --no-repo-update

import UIKit
import SDWebImage
private let edgeMargin : CGFloat = 15
private let itemMargin : CGFloat = 10
class HomeViewCell: UITableViewCell {

    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var picView: PicCollectionView!
    @IBOutlet weak var retweetedContentLabel: UILabel!
    
    @IBOutlet weak var picViewWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewHCons: NSLayoutConstraint!
    
    // MARK:- 自定义属性
    
    var viewModel :StatuesViewModel?{
     
        didSet{
            guard let viewModel = viewModel else {
                return
            }
            // 2.设置头像
            iconView.sd_setImage(with: viewModel.profileURL, placeholderImage: UIImage(named: "avatar_default_small"))
            
            // 3.设置认证的图标
            verifiedView.image = viewModel.verifiedImage
            
            // 4.昵称
            screenNameLabel.text = viewModel.status?.user?.screen_name
            
            // 5.会员图标
            vipView.image = viewModel.vipImage
            
            // 6.设置时间的Label
            timeLabel.text = viewModel.createAtText
            
            // 7.设置来源
            if let sourceText = viewModel.sourceText {
                sourceLabel.text = "来自 " + sourceText
            } else {
                sourceLabel.text = nil
            }
            
            // 8.设置昵称的文字颜色
            screenNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            
            // 9.计算picView的宽度和高度的约束
            let picViewSize = calculatePicViewSize(count: viewModel.picURLs.count)
            picViewWCons.constant = picViewSize.width
            picViewHCons.constant = picViewSize.height
            
            //10.将picURL数据传递给picView
            picView.picURLs = viewModel.picURLs
            
            //11.设置转发微博正文
            
            if viewModel.status?.retweeted_status != nil {
                if let screenName = viewModel.status?.retweeted_status?.user?.screen_name,let retweetedText = viewModel.status?.retweeted_status?.text {
                    retweetedContentLabel.text =  "@" + "\(screenName) :" + retweetedText
                }else{
                     retweetedContentLabel.text = ""
                }
            }

        }
    }
    
}


// MARK:- 计算图片的大小
extension HomeViewCell{
    
    func calculatePicViewSize(count : Int) -> CGSize {
        
        // 0.取出picView对应的layout
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        
        // 2.计算出来imageViewWH
        let imageViewWH = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * itemMargin) / 3
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
        
        // 1.没有配图
        if count == 0 {
            return CGSize.zero
        }
        
        // 2. 一张配图
        if count == 1 {
            
            let urlString = viewModel?.picURLs.last?.absoluteString
            
            let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: urlString)
           layout.itemSize = CGSize(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
            
            return layout.itemSize
            
        }
        // 3.四张配图
        if count == 4 {
            
            let picViewWH = imageViewWH * 2 + itemMargin + 0.1
            return CGSize(width: picViewWH, height: picViewWH)
        }
        
        // 4.其他张配图
        // 4.1.计算行数
        let rows = CGFloat((count - 1) / 3 + 1)
        
        // 4.2.计算picView的高度
        let picViewH = rows * imageViewWH + (rows - 1) * itemMargin
        
        // 4.3.计算picView的宽度
        let picViewW = UIScreen.main.bounds.width - 2 * edgeMargin
        
        return CGSize(width: picViewW, height: picViewH)
    

    }
}


